<template>
  <div id="poster" style="background-image: url('./images/OIP-C (1).jpg');
        background-size: cover;">
    <el-form
    :model="ruleForm"
    :rules="rules"
    ref="ruleForm"
    label-width="0px"
    label-position="left"
    class="register-container">
    <h3 class="register_title">
        系统注册
        <el-button size="mini" style="position: relative; left: 60px;" @click="toLogin()">登录
            <i class="el-icon-arrow-right el-icon--right"></i>
        </el-button>
    </h3>
    <el-form-item prop="username">
        <el-input
        type="text"
        autocomplete="off"
        v-model="ruleForm.username"
        placeholder="请输入用户名"
        prefix-icon="el-icon-user-solid"></el-input>
    </el-form-item>
    <el-form-item prop="email">
        <el-input
        type="text"
        autocomplete="off"
        v-model="ruleForm.email"
        placeholder="请输入邮箱地址"
        prefix-icon="el-icon-message"></el-input>
    </el-form-item>
    <el-form-item prop="password">
        <el-input
        type="password"
        autocomplete="off"
        v-model="ruleForm.password"
        placeholder="请输入密码"
        prefix-icon="el-icon-lock"></el-input>
    </el-form-item>
    <el-form-item prop="checkPass">
        <el-input
        type="password"
        autocomplete="off"
        v-model="ruleForm.checkPass"
        placeholder="请输入用户名"
        prefix-icon="el-icon-lock"></el-input>
    </el-form-item>
    <el-form-item>
        <el-button type="primary" style="background:#505458;border:none" @click=submitForm(ruleForm)>注册</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
    </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
    name:'Register',
    data(){
        var validatePass = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入密码'));
            }else {
                if (this.ruleForm.checkPass !== '') {
                    this.$refs.ruleForm.validateField('checkPass');
                }
                callback();
            }
        };
        var validatePass2 = (rule, value, callback) => {
            if (value ==='') {
                callback(new Error('请再次输入密码'));
            }else if(value !== this.ruleForm.password) {
                callback(new Error('两次输入密码不一致！'));
            } else {
                callback();
            }
        };
        return {
            ruleForm: {
                username: '',
                password: '',
                email: '',
                checkPass: ''
            },
            rules: {
                username:[
                    {required: true, message:"请输入你的名称", trigger: 'blur'},
                    {min:2, max:9,message:"长度2到9个字符",trigger:'blur'}
                ],
                password:[
                    {validator: validatePass, trigger: 'blur'}
                ],
                checkPass:[
                    {validator: validatePass2, trigger: 'blur'}
                ]
            }
        };
    },
    methods: {
        submitForm(ruleform) {
            this.axios.post('http://127.0.0.1:8000/user/register/',this.ruleForm)
            .then((resp) =>{
                let data=resp.data;
                if(resp.status==200){
                    this.ruleForm = {};
                    this.$message({
                    message: '注册成功!',
                    type: 'success'
                    });
                    this.$router.push({path:'/Login'})
                }
            })
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
        toLogin(){
            this.$router.push({path:'/Login'})
        }
    }
}
</script>

<style>
#poster{
    background-position: center;
    height: 100%;
    width: 100%;
    background-size: cover;
    position: fixed;
    margin: 0px;
    padding: 0px;
}
.register-container{
    border-radius: 15px;
    background-clip: padding-box;
    margin:140px auto;
    width: 350px;
    padding: 35px 35px 15px 35px;
    background: #fff;
    border: 1px solid #eaeaea;
    box-shadow: 0 0 25px #cac6cc;
}
.register_title{
    position: relative;
    margin: 10px auto 40px auto ;
    text-align: center;
    left: 35px;
    color: #505458;
}
</style>
